package com.cloudspace.yunloan.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudspace.yunloan.R;


public class ActionSheet extends Dialog implements OnClickListener {
    protected Context mContext;
    protected LinearLayout mPanel;
    protected Attributes mAttrs;
    public static String mCancelButtonTitle;
    public static String mTittle;
    public static String[] mOtherButtonTitles;
    public static int mActionId;
    protected ActionSheetListener mListener;

    public ActionSheet(Context context, int theme) {
        super(context, theme);
        this.mContext = context;
        init();
    }

    public void init() {
        Window w = getWindow();
        WindowManager.LayoutParams dialogParams = w.getAttributes();
        dialogParams.x = 0;
        dialogParams.gravity = Gravity.BOTTOM;
        onWindowAttributesChanged(dialogParams);
        setCanceledOnTouchOutside(true);
        mAttrs = readAttribute();
        mPanel = createView();
        mPanel.setMinimumWidth(10000);//maybe is a bug
        mPanel.setPadding(mAttrs.padding, mAttrs.padding, mAttrs.padding, mAttrs.padding);
        createItems();
        setContentView(mPanel);
    }

    protected LinearLayout createView() {
        return (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.actionsheet, null);
    }

    private void createItems() {
        String[] titles = getOtherButtonTitles();
        if (!StringUtil.isEmpty(mTittle)) {
            TextView title = new TextView(mContext);
            title.setTextColor(Color.parseColor("#666666"));
            title.setTextSize(Convert.getFloatFromResource(mContext, R.dimen.small_text_size_float));
            title.setText(mTittle);
            title.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams params = createButtonLayoutParams();
            params.setMargins(0, 0, 0, Convert.getDimensionPixelFromResource(mContext,
                    R.dimen.dimen_6_dip));
            mPanel.addView(title, params);
        }
        float textSize = Convert
                .getFloatFromResource(mContext, R.dimen.game_detail_small_text_float);
        if (titles != null) {
            for (int i = 0; i < titles.length; i++) {
                Button bt = new Button(mContext);
                bt.setTag(R.id.dialog_index_tag, i);
                bt.setOnClickListener(this);
                bt.setBackgroundDrawable(getOtherButtonBg(titles, i));
                bt.setText(titles[i]);
                bt.setGravity(Gravity.CENTER);
                bt.setTextColor(mAttrs.otherButtonTextColor);
                bt.setTextSize(textSize);
                if (i > 0) {
                    LinearLayout.LayoutParams params = createButtonLayoutParams();
                    params.topMargin = mAttrs.otherButtonSpacing;
                    mPanel.addView(bt, params);
                } else {
                    mPanel.addView(bt);
                }
            }
        }
        if (!TextUtils.isEmpty(getCancelButtonTitle())) {
            Button bt = new Button(mContext);
            bt.setTextSize(textSize);
            bt.setId(R.id.dialog_cancel_id);
            bt.setBackgroundDrawable(mAttrs.cancelButtonBackground);
            bt.setText(getCancelButtonTitle());
            bt.setGravity(Gravity.CENTER);
            bt.setTextColor(mAttrs.cancelButtonTextColor);
            bt.setOnClickListener(this);
            LinearLayout.LayoutParams params = createButtonLayoutParams();
            params.topMargin = mAttrs.cancelButtonMarginTop;
            mPanel.addView(bt, params);
        }
    }

    public LinearLayout.LayoutParams createButtonLayoutParams() {
        return new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    }

    public int getAcionId() {
        return mActionId;
    }

    private Drawable getOtherButtonBg(String[] titles, int i) {
        if (titles.length == 1) {
            return mAttrs.otherButtonSingleBackground;
        }
        if (titles.length == 2) {
            switch (i) {
                case 0:
                    return mAttrs.otherButtonTopBackground;
                case 1:
                    return mAttrs.otherButtonBottomBackground;
            }
        }
        if (titles.length > 2) {
            if (i == 0) {
                return mAttrs.otherButtonTopBackground;
            }
            if (i == (titles.length - 1)) {
                return mAttrs.otherButtonBottomBackground;
            }
            return mAttrs.getOtherButtonMiddleBackground();
        }
        return null;
    }

    private Attributes readAttribute() {
        Attributes attrs = new Attributes(mContext);
        mContext.setTheme(R.style.ActionSheetStyleIOS7);
        TypedArray a = mContext.getTheme().obtainStyledAttributes(null,
                R.styleable.ActionSheet, R.attr.actionSheetStyle, 0);
        Drawable background = a.getDrawable(R.styleable.ActionSheet_actionSheetBackground);
        if (background != null) {
            attrs.background = background;
        }
        Drawable cancelButtonBackground = a.getDrawable(R.styleable.ActionSheet_cancelButtonBackground);
        if (cancelButtonBackground != null) {
            attrs.cancelButtonBackground = cancelButtonBackground;
        }
        Drawable otherButtonTopBackground = a.getDrawable(R.styleable.ActionSheet_otherButtonTopBackground);
        if (otherButtonTopBackground != null) {
            attrs.otherButtonTopBackground = otherButtonTopBackground;
        }
        Drawable otherButtonMiddleBackground = a.getDrawable(R.styleable.ActionSheet_otherButtonMiddleBackground);
        if (otherButtonMiddleBackground != null) {
            attrs.otherButtonMiddleBackground = otherButtonMiddleBackground;
        }
        Drawable otherButtonBottomBackground = a.getDrawable(R.styleable.ActionSheet_otherButtonBottomBackground);
        if (otherButtonBottomBackground != null) {
            attrs.otherButtonBottomBackground = otherButtonBottomBackground;
        }
        Drawable otherButtonSingleBackground = a.getDrawable(R.styleable.ActionSheet_otherButtonSingleBackground);
        if (otherButtonSingleBackground != null) {
            attrs.otherButtonSingleBackground = otherButtonSingleBackground;
        }
        attrs.cancelButtonTextColor = a.getColor(
                R.styleable.ActionSheet_cancelButtonTextColor,
                attrs.cancelButtonTextColor);
        attrs.otherButtonTextColor = a.getColor(
                R.styleable.ActionSheet_otherButtonTextColor,
                attrs.otherButtonTextColor);
        attrs.padding = (int) a.getDimension(
                R.styleable.ActionSheet_actionSheetPadding, attrs.padding);
        attrs.otherButtonSpacing = (int) a.getDimension(
                R.styleable.ActionSheet_otherButtonSpacing,
                attrs.otherButtonSpacing);
        attrs.cancelButtonMarginTop = (int) a.getDimension(
                R.styleable.ActionSheet_cancelButtonMarginTop,
                attrs.cancelButtonMarginTop);
        attrs.actionSheetTextSize = a.getDimension(
                R.styleable.ActionSheet_actionSheetTextSize,
                attrs.actionSheetTextSize);

        a.recycle();
        return attrs;
    }

    private String getCancelButtonTitle() {
        return mCancelButtonTitle;
    }

    private String[] getOtherButtonTitles() {
        return mOtherButtonTitles;
    }

    public void setActionSheetListener(ActionSheetListener listener) {
        mListener = listener;
    }

    @Override
    public void onClick(View v) {
        dismiss();
        if (v.getId() == R.id.dialog_cancel_id) {
            if (mListener != null) {
                mListener.onCancel(this);
            }
        } else if (v.getId() == R.id.dialog_bg_view) {
            if (mListener != null) {
                mListener.onDismiss(this);
                mTittle = null;
            }
        } else {
            if (mListener != null) {
                mListener.onItemClick(this, Integer.valueOf(v.getTag(R.id.dialog_index_tag).toString()));
            }
        }
    }

    public static Builder createBuilder(Context context) {
        return new Builder(context);
    }

    private static class Attributes {
        private Context mContext;

        public Attributes(Context context) {
            mContext = context;
            this.background = new ColorDrawable(Color.TRANSPARENT);
            this.cancelButtonBackground = new ColorDrawable(Color.BLACK);
            ColorDrawable gray = new ColorDrawable(Color.GRAY);
            this.otherButtonTopBackground = gray;
            this.otherButtonMiddleBackground = gray;
            this.otherButtonBottomBackground = gray;
            this.otherButtonSingleBackground = gray;
            this.cancelButtonTextColor = Color.WHITE;
            this.otherButtonTextColor = Color.BLACK;
            this.padding = mContext.getResources().getDimensionPixelSize(R.dimen.dimen_11_dip);
            this.otherButtonSpacing = mContext.getResources().getDimensionPixelSize(R.dimen.dimen_1_dip);
            this.cancelButtonMarginTop = mContext.getResources().getDimensionPixelSize(R.dimen.dimen_5_dip);
            this.actionSheetTextSize = 14;//16px
        }

        public Drawable getOtherButtonMiddleBackground() {
            if (otherButtonMiddleBackground instanceof StateListDrawable) {
                TypedArray a = mContext.getTheme().obtainStyledAttributes(null,
                        R.styleable.ActionSheet, R.attr.actionSheetStyle, 0);
                otherButtonMiddleBackground = a
                        .getDrawable(R.styleable.ActionSheet_otherButtonMiddleBackground);
                a.recycle();
            }
            return otherButtonMiddleBackground;
        }

        Drawable background;
        Drawable cancelButtonBackground;
        Drawable otherButtonTopBackground;
        Drawable otherButtonMiddleBackground;
        Drawable otherButtonBottomBackground;
        Drawable otherButtonSingleBackground;
        int cancelButtonTextColor;
        int otherButtonTextColor;
        int padding;
        int otherButtonSpacing;
        int cancelButtonMarginTop;
        float actionSheetTextSize;
    }

    public static class Builder {

        private Context mContext;
        private ActionSheetListener mListener;

        public Builder(Context context) {
            mTittle = null;
            mCancelButtonTitle = null;
            mContext = context;
        }

        public Builder hideCancelButton() {
            mCancelButtonTitle = "";
            return this;
        }

        public Builder setCancelButtonTitle(String title) {
            mCancelButtonTitle = title;
            return this;
        }

        public Builder setOtherButtonTitles(String... titles) {
            mOtherButtonTitles = titles;
            return this;
        }

        public Builder setListener(ActionSheetListener listener) {
            this.mListener = listener;
            return this;
        }

        public Builder setTitle(String title) {
            mTittle = title;
            return this;
        }

        public Builder setActionId(int id) {
            mActionId = id;
            return this;
        }

        public ActionSheet show() {
            if (mCancelButtonTitle == null) {
                mCancelButtonTitle = mContext.getString(R.string.cancel);
            }

            ActionSheet actionSheet = new ActionSheet(mContext, R.style.ActionSheet);
            actionSheet.setActionSheetListener(mListener);
//			actionSheet.setCancelable(true);
//			actionSheet.setCanceledOnTouchOutside(true);
            actionSheet.show();
            return actionSheet;
        }

    }

    public static abstract class SimpleActionSheetListener implements ActionSheetListener {
        @Override
        public void onCancel(ActionSheet actionSheet) {
        }

        @Override
        public void onDismiss(ActionSheet actionSheet) {
        }
    }

    public static interface ActionSheetListener {

        void onItemClick(ActionSheet actionSheet, int index);

        void
        onCancel(ActionSheet actionSheet);

        void onDismiss(ActionSheet actionSheet);

    }

}